package com.fand.platform.authority.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.authority.dto.ResourceDto;
import com.fand.platform.authority.dto.ResourceVaRuTrMoDto;
import com.fand.platform.authority.service.AuthorityService;
import com.fand.platform.authority.validator.ResourceVaRuTrMoValidator;
import com.fand.platform.authority.validator.ResourceValidator;
import com.fand.platform.authority.vo.ResourceVo;
import com.google.common.collect.Maps;

/**
 * 资源
 * 
 * @author yaoHT
 */
@Controller
public class ResourceController {
	@Qualifier(AuthorityService.BEAN_ID)
	@Autowired(required = false)
	private AuthorityService authorityService;

	@Qualifier(ResourceValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator resourceValidator;

	@Qualifier(ResourceVaRuTrMoValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator resourceVaRuTrMoValidator;

	@RequestMapping(value = { "/admin/authority/resource/resource/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminResourceDelete(final ResourceVo resourceVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getResourceService().deleteResourceBy2(resourceVo.getResource());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/resource/resource/edit.html" }, method = RequestMethod.GET)
	public String adminResourceEdit(final ResourceVo resourceVo, final Model model) {
		final ResourceDto resource = this.authorityService.getResourceService().getResourceBy2(
				resourceVo.getResource());

		model.addAttribute("resource", resource);

		return "/admin/authority/resource/resource/edit";
	}

	@RequestMapping(value = { "/admin/authority/resource/resource.html",
			"/admin/authority/resource/resource/index.html" })
	public String adminResourceIndex() {
		return "/admin/authority/resource/resource/index";
	}

	@RequestMapping(value = { "/admin/authority/resource/resource/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminResourceIndexPagination(final ResourceVo resourceVo) {
		final PaginationDto<Map<String, Object>> resourcePagination = this.authorityService.getResourceService()
				.paginationResourceBy1ForMapList(resourceVo.getResource());

		final Map<String, Object> resourcePaginationMap = Maps.newHashMap();

		resourcePaginationMap.put("total", resourcePagination.getTotalSize());
		resourcePaginationMap.put("rows", resourcePagination.getDataList());

		return resourcePaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/resource/resource/new.html" }, method = RequestMethod.GET)
	public String adminResourceNew(final ResourceVo resourceVo, final Model model) {
		model.addAttribute("resource", resourceVo.getResource());

		return "/admin/authority/resource/resource/new";
	}

	@RequestMapping(value = { "/admin/authority/resource/resource/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminResourceSave(final ResourceVo resourceVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.resourceValidator.validate(resourceVo.getResource(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getResourceService().saveResourceBy1ForResource(resourceVo.getResource());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/resource/resource/validationBy1.json" })
	public @ResponseBody
	Boolean adminResourceValidationBy1(final ResourceVo resourceVo) {
		try {
			final int count = this.authorityService.getResourceService().readResourceBy1ForInteger(
					resourceVo.getResource());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminResourceVaRuTrMoDelete(final ResourceVo resourceVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getResourceService().deleteResourceVaRuTrMoBy2(resourceVo.getResourceVaRuTrMo());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo/edit.html" }, method = RequestMethod.GET)
	public String adminResourceVaRuTrMoEdit(final ResourceVo resourceVo, final Model model) {
		final ResourceVaRuTrMoDto resourceVaRuTrMo = this.authorityService.getResourceService()
				.getResourceVaRuTrMoBy2(resourceVo.getResourceVaRuTrMo());

		model.addAttribute("resourceVaRuTrMo", resourceVaRuTrMo);

		return "/admin/authority/resource/resourceVaRuTrMo/edit";
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo.html",
			"/admin/authority/resource/resourceVaRuTrMo/index.html" })
	public String adminResourceVaRuTrMoIndex() {
		return "/admin/authority/resource/resourceVaRuTrMo/index";
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminResourceVaRuTrMoIndexPagination(final ResourceVo resourceVo) {
		final PaginationDto<Map<String, Object>> resourceVaRuTrMoPagination = this.authorityService
				.getResourceService().paginationResourceVaRuTrMoBy1ForMapList(resourceVo.getResourceVaRuTrMo());

		final Map<String, Object> resourceVaRuTrMoPaginationMap = Maps.newHashMap();

		resourceVaRuTrMoPaginationMap.put("total", resourceVaRuTrMoPagination.getTotalSize());
		resourceVaRuTrMoPaginationMap.put("rows", resourceVaRuTrMoPagination.getDataList());

		return resourceVaRuTrMoPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo/new.html" }, method = RequestMethod.GET)
	public String adminResourceVaRuTrMoNew(final ResourceVo resourceVo, final Model model) {
		model.addAttribute("resourceVaRuTrMo", resourceVo.getResourceVaRuTrMo());

		return "/admin/authority/resource/resourceVaRuTrMo/new";
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminResourceVaRuTrMoSave(final ResourceVo resourceVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.resourceVaRuTrMoValidator.validate(resourceVo.getResourceVaRuTrMo(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getResourceService().saveResourceVaRuTrMoBy1ForResourceVaRuTrMo(
					resourceVo.getResourceVaRuTrMo());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo/validationBy1.json" })
	public @ResponseBody
	Boolean adminResourceVaRuTrMoValidationBy1(final ResourceVo resourceVo) {
		try {
			final int count = this.authorityService.getResourceService().readResourceVaRuTrMoBy1ForInteger(
					resourceVo.getResourceVaRuTrMo());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/resource/resourceVaRuTrMo/view.html" }, method = RequestMethod.GET)
	public String adminResourceVaRuTrMoView(final ResourceVo resourceVo, final Model model) {
		final ResourceVaRuTrMoDto resourceVaRuTrMo = this.authorityService.getResourceService()
				.getResourceVaRuTrMoBy2(resourceVo.getResourceVaRuTrMo());

		model.addAttribute("resourceVaRuTrMo", resourceVaRuTrMo);

		return "/admin/authority/resource/resourceVaRuTrMo/view";
	}

	@RequestMapping(value = { "/admin/authority/resource/resource/view.html" }, method = RequestMethod.GET)
	public String adminResourceView(final ResourceVo resourceVo, final Model model) {
		final ResourceDto resource = this.authorityService.getResourceService().getResourceBy2(
				resourceVo.getResource());

		model.addAttribute("resource", resource);

		return "/admin/authority/resource/resource/view";
	}
}
